﻿
namespace Leetcode.N0467.V4;

public class Solution
{
    public int FindSubstringInWraproundString(string p)
    {
        var maxLengths = new int[26];
        var length = 0;

        for (int i = p.Length - 1; i >= 0; i--)
        {
            length = (i < p.Length - 1 && (p[i + 1] - p[i] + 26) % 26 == 1) ? length + 1 : 1;
            maxLengths[p[i] - 'a'] = Math.Max(maxLengths[p[i] - 'a'], length);
        }
        return maxLengths.Sum(_ => _);
    }
}